package nc.bs.util;

import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.pub.BusinessException;

public class DefdocUtils {
	private static BaseDAO dao;

	/**
	 * 获取自定义档案信息 通过自定义档案编码
	 * @param defdocCode 自定义档案编码
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String,Object>> getDefdoc(Object defdocCode) throws BusinessException{
		String sql = buildDefdocSQL(defdocCode);
		List<Map<String,Object>> listmaps = (List<Map<String,Object>>)getDao().executeQuery(sql, new MapListProcessor());
		return listmaps != null && !listmaps.isEmpty() && listmaps.size() > 0 ? listmaps : null;
	}


	/**
	 * 获取自定义档案信息 通过自定义档案编码
	 * @param defdocCode 自定义档案编码
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public static List<String> getListDefdocCode(Object defdocCode) throws BusinessException{
		String sql = buildDefdocCodeSQL(defdocCode);
		List<String> listmaps = (List<String>)getDao().executeQuery(sql, new ColumnListProcessor());
		return listmaps != null && !listmaps.isEmpty() && listmaps.size() > 0 ? listmaps : null;
	}

	/**
	 * 获取自定义档案信息 通过自定义档案编码
	 * @param defdocCode 自定义档案编码
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public static List<String> getListDefdocName(Object defdocCode) throws BusinessException{
		String sql = buildDefdocNameSQL(defdocCode);
		List<String> listmaps = (List<String>)getDao().executeQuery(sql, new ColumnListProcessor());
		return listmaps != null && !listmaps.isEmpty() && listmaps.size() > 0 ? listmaps : null;
	}
	
	/**
	 * 获取自定义档案信息 通过自定义档案编码和档案编码
	 * @param defdocCode  自定义档案编码
	 * @param code        档案编码
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object> getDefdoc(Object defdocCode, Object code) throws BusinessException{
		String sql = buildDefdocSQL(defdocCode,code);
		Map<String,Object> map = (Map<String,Object>)getDao().executeQuery(sql, new MapProcessor());
		return map != null && !map.isEmpty() ? map : null;
	}
	
	/**
	 * 获取自定义档案信息 通过自定义档案编码和客户编码  要求自定义档案中档案编码必须与 客户编码一直
	 * @param defdocCode
	 * @param pk_customer
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object>  getDefdocLinkCustomer(Object defdocCode, Object pk_customer) throws BusinessException{
		String sql = buildDefdocLinkCustomerSQL(defdocCode, pk_customer);
		Map<String,Object> map = (Map<String,Object>)getDao().executeQuery(sql, new MapProcessor());
		return map != null && !map.isEmpty() && map.size() > 0 ? map : null;
	}
	
	/**
	 * 获取自定义档案中维护的票折率信息
	 * @param defdocCode    自定义档案编码
	 * @param defdocName    自定义档案名称
	 * @param customername  客户名称需要与组织名称一直
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object>  getDefdocSQLByDefCodeAndNyAndCustCode(Object defdocCode,Object defdocName, Object customername) throws BusinessException{
		String sql = buildDefdocSQLByDefCodeAndNyAndCustCode(defdocCode, defdocName, customername);
		Map<String,Object> map = (Map<String,Object>)getDao().executeQuery(sql, new MapProcessor());
		return map != null && !map.isEmpty() && map.size() > 0 ? map : null;
	}
	
	/**
	 * 构建自定义档案查询sql
	 * @param defdocCode
	 * @return
	 * @throws BusinessException
	 */
	public static String buildDefdocSQL(Object defdocCode) throws BusinessException{
        String sql = " select bd_defdoc.*     " +
                "  from bd_defdoc     " +
                "  left join bd_defdoclist on bd_defdoc.pk_defdoclist = bd_defdoclist.pk_defdoclist     " +
                " where bd_defdoc.dr = 0 and bd_defdoc.enablestate = 2     " +
                "   and bd_defdoclist.dr = 0      " +
                "   and bd_defdoclist.code='" + defdocCode + "'";
		return sql;
	}
	/**
	 * 构建自定义档案查询sql
	 * @param defdocCode
	 * @return
	 * @throws BusinessException
	 */
	public static String buildDefdocCodeSQL(Object defdocCode) throws BusinessException{
        String sql = " select bd_defdoc.code     " +
                "  from bd_defdoc     " +
                "  left join bd_defdoclist on bd_defdoc.pk_defdoclist = bd_defdoclist.pk_defdoclist     " +
                " where bd_defdoc.dr = 0 and bd_defdoc.enablestate = 2     " +
                "   and bd_defdoclist.dr = 0      " +
                "   and bd_defdoclist.code='" + defdocCode + "'";
		return sql;
	}
	/**
	 * 构建自定义档案查询sql
	 * @param defdocCode
	 * @return
	 * @throws BusinessException
	 */
	public static String buildDefdocNameSQL(Object defdocCode) throws BusinessException{
        String sql = " select bd_defdoc.name     " +
                "  from bd_defdoc     " +
                "  left join bd_defdoclist on bd_defdoc.pk_defdoclist = bd_defdoclist.pk_defdoclist     " +
                " where bd_defdoc.dr = 0 and bd_defdoc.enablestate = 2     " +
                "   and bd_defdoclist.dr = 0      " +
                "   and bd_defdoclist.code='" + defdocCode + "'";
		return sql;
	}
	/**
	 * 
	 * 构建自定义档案查询sql
	 * @param defdocCode
	 * @param code
	 * @return
	 * @throws BusinessException
	 */
	public static String buildDefdocSQL(Object defdocCode, Object code ) throws BusinessException{
        String sql = " select bd_defdoc.*     " +
                "  from bd_defdoc     " +
                "  left join bd_defdoclist on bd_defdoc.pk_defdoclist = bd_defdoclist.pk_defdoclist     " +
                " where bd_defdoc.dr = 0 and bd_defdoc.enablestate = 2     " +
                "   and bd_defdoclist.dr = 0      " +
                "   and bd_defdoclist.code='" + defdocCode + "'" +
                "   and bd_defdoc.code ='" + code + "' ";
		return sql;
	}

	/**
	 * 指定组织、档案编码、档案名称、自定义档案列表def1引用了自动档案查询
	 * @param defdocCode   自定义档案编码
	 * @param ny           自定义档案名称-年月(yyyy-MM)
	 * @param customername 客户名称必须与组织名称一致
	 * @return
	 * @throws BusinessException
	 */
	public static  String buildDefdocSQLByDefCodeAndNyAndCustCode(Object defdocCode,Object ny,Object customername) throws BusinessException{
        String sql = "select " +
                "	bd.code customer, bd.name yearandmonth, bd.shortname pzl,ms.code    " +
                "FROM " +
                "	bd_defdoc  bd   " +
                " INNER JOIN bd_defdoclist bdl on bd.pk_defdoclist = bdl.pk_defdoclist   " +
                " inner join org_orgs oos on oos.pk_org =  bd.pk_org   " +
                " inner join bd_defdoc ms on bd.def1 = ms.pk_defdoc  " +
                "WHERE 1 = 1 AND ( 1 = 1 AND 1 = 1 AND ( bd.enablestate = 2 ) )   " +
                "	AND bdl.code = '" + defdocCode + "'   " +
                "	AND oos.name = '" + customername + "'   " +
                "	and bd.name = '" + ny + "'";
		return sql;
	}
	
	/**
	 * 构建自定义档案与客户信息关联sql
	 * @param defdocCode  自定义档案编码 
	 * @param pk_customer 客户信息主键
	 * @return
	 * @throws BusinessException
	 */
	public static String buildDefdocLinkCustomerSQL(Object defdocCode, Object pk_customer) throws BusinessException{
        String sql = "select bd_defdoc.*    " +
                "  from bd_defdoc    " +
                "  left join bd_defdoclist on bd_defdoc.pk_defdoclist = bd_defdoclist.pk_defdoclist    " +
                "  left join bd_customer bc on bc.code = bd_defdoc.code     " +
                " where bd_defdoc.dr = 0 and bd_defdoc.enablestate = 2    " +
                "   and bd_defdoclist.dr = 0  and bc.dr = 0    " +
                "	 and bc.pk_customer = '" + pk_customer + "'    " +
                "   and bd_defdoclist.code='" + defdocCode + "'";
		return sql;
	}
	
	public static BaseDAO getDao() {
		return dao == null ? new BaseDAO() : dao;
	}
}
